package com.liukai.novel.activity;

import android.content.Context;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.liukai.novel.R;
import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_base.base.BaseFragment;
import com.liukai.novel_rack.fragment.NovelRackFragment;
import com.liukai.novel_type.fragment.NovelTypeFragment;
import com.liukai.user.fragment.MyInfoFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.mainFrameLayout)
    FrameLayout mFrameLayout;

    @BindView(R.id.bottomNavigationView)
    BottomNavigationView mBottomNavigationView;
    private NovelRackFragment mNovelRackFragment;
    private NovelTypeFragment mNovelTypeFragment;
    private MyInfoFragment mMyInfoFragment;


    /**
     * 获取布局Layout的Id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData(Context context) {

    }

    /**
     * 初始化组件
     */
    @Override
    protected void initView() {
        mNovelRackFragment = new NovelRackFragment();
        mNovelTypeFragment = new NovelTypeFragment();
        mMyInfoFragment = new MyInfoFragment();
        switchFragment(mNovelRackFragment);
    }

    /**
     * 初始化监听器
     */
    @Override
    protected void initListener() {

        // 底部导航栏点击事件
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    // 点击书架按钮，切换至书架页面
                    case R.id.book_rack:
                        switchFragment(mNovelRackFragment);
                        break;

                        // 点击分类按钮，切换至分类页面
                    case R.id.book_type:
                        switchFragment(mNovelTypeFragment);
                        break;

                        // 点击我的按钮，切换至我的页面
                    case R.id.my_info:
                        switchFragment(mMyInfoFragment);
                        break;

                    default:
                        Toast.makeText(MainActivity.this, "点击失败", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 销毁Activity释放资源
     */
    @Override
    protected void release() {
        super.release();
    }

    /**
     * 切换Fragment
     *
     * @param baseFragment
     */
    private void switchFragment(BaseFragment baseFragment) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.mainFrameLayout, baseFragment);
        fragmentTransaction.commit();

    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        LogUtils.e(getClass(),"resultCode" + resultCode);
//        LogUtils.e(getClass(),"requestCode" + requestCode);
//    }


}
